from typing import cast
from django.db import models
from django.db.models import expressions

# Create your models here.

Genders = [
    (0, '女'),
    (1, '男'),
]

class Teachers(models.Model):
    name = models.CharField(max_length=64, blank=False, unique=True, verbose_name='姓名')
    gender = models.SmallIntegerField(blank=False, choices=Genders, verbose_name='性别')
    experience = models.FloatField(blank=False, verbose_name='经验')
    info = models.CharField(max_length=1024, blank=True, verbose_name='自我介绍')

    class Meta:
        verbose_name = '教师'
        verbose_name_plural = '教师列表'


    @classmethod
    def get_all(cls):
        return cls.objects.all()

    @classmethod
    def get_one(cls, data_id):
        try:
            return cls.objects.get(pk=data_id)
        except cls.DoesNotExist:
            return


    @classmethod
    def delete_data(cls, data_id):
        try:
            query = cls.objects.delete(data_id)
            return
        except cls.DoesNotExist:
            return

    @classmethod
    def get_list_by_query(cls, **kwargs):
        filters = {}
        if kwargs.get("name"):
            filters["name"] = kwargs.get("name")
        if kwargs.get("names"):
            filters["name__contains"] = kwargs.get("names")
        if kwargs.get("gender"):
            filters["gender"] = kwargs.get("gender")
        if kwargs.get("experience"):
            filters["experience"] = kwargs.get("experience")
        return cls.objects.filter(**filters)


    @classmethod
    def insert_one(cls, insert_data):
        return cls.objects.create(
            name=insert_data.get("name"),
            gender=insert_data.get("gender"),
            experience = insert_data.get("experience"),
            infor = insert_data.get("info")
        )

    @classmethod
    def update_one(cls, pk, **kwargs):
        obj = cls.get_one(pk)
        if not obj:
            return
        if kwargs.get("name"):
            obj.name = kwargs.get("name")
        if kwargs.get("gender"):
            obj.name = kwargs.get("gender")
        if kwargs.get("experience"):
            obj.name = kwargs.get("experience")
        if kwargs.get("info"):
            obj.name = kwargs.get("info")

        obj.save()